package com.kotlin.lib.coroutines

import kotlinx.coroutines.*
import kotlin.random.Random

lateinit var onFailure: (Int, String) -> Unit

fun main() = runBlocking<Unit> {

    val job = GlobalScope.launch(Dispatchers.Default) {
        delay(1000)
        println(" world !")

        arrayOf(2, 1, 3, 4, 6, 5).forEach {
            launch(Dispatchers.Default) {
                print(it)
            }
        }
    }
    println("hello")
    job.join()

}

private suspend fun print(v: Int) = coroutineScope {
    val l = 100L * Random.Default.nextInt(9)
    println("$v :start, spend $l  ${Thread.currentThread().name}")
    delay(l)
    println("               $v ,${Thread.currentThread().name}")
}